<?php
/**
 * @return
 * @author 朱乐斌
 * @date
 */

namespace app\api\controller;

use app\api\model\EducationModel;
use app\api\model\ResumeModel;
use app\api\model\SchoolModel;
use app\api\model\UserGxModel;
use app\Request;

class Educational extends Base
{


    // 用户获取教育经历
    public function educationalList(Request $request)
    {
        try {
            // 获取当前用户id
            $user = UserGxModel::where('phone',$request->phone)->find();
            if ($user['type'] == 2) return errMsg('嘿嘿你才为什么不让你查看');
            // 获取简历id
            $resume = ResumeModel::where('user_id','=',$user->id)->field('id')->find();
            if ($resume == null) return errMsg('请先完善简历');
            $edu = educationModel::where('resume_id',$resume->id)->select()->toArray();
            if (empty($edu)) return errMsg('暂无教育经历');
            return succMsg($edu);
        }catch (\Exception $e){
            return errMsg($e->getMessage());
        }
    }
    // 添加教育经历
    public function addEducational(Request $request)
    {
        // 接收数据
        $data = $this->request->post();
        try {
            $user = UserGxModel::where('phone',$request->phone)->find();
            if ($user['type'] == 2) return errMsg('嘿嘿你猜为什么不让添加');
            // 获取简历id
            $resume = ResumeModel::where('user_id',$user->id)->field('id')->find();
            // 关联了学校表id，用户输入学校名称搜索出学校的id
            $school = $data['school_name'];
            $schoolModel = SchoolModel::where('name','=',$school)->field('id,name,type')->find();
            if (empty($schoolModel)) return errMsg('请输入正确的学校');
            if ($data['qualifications'] !== $schoolModel['type']) return errMsg('请给你的学校虚选择正确的学历');
            // 添加前查询判断有没有重复添加，每个用户只能添加一个小学初中高中或职校，大学
            $eduData = [
                'school_id' => $schoolModel['id'],
                'resume_id' => $resume['id'],
                'speciality' => $data['speciality'],
                'qualifications' => $data['qualifications'],
                'create_time' => date('Y-m-d H:i:s'),
                'end_time' => date('Y-m-d H:i:s'),
            ];
            $educationModel = EducationModel::where('school_id',$schoolModel['id'])->find();
            if (!empty($educationModel)) return errMsg('不要重复添加');
            // 存入数据库
            $educationModel = EducationModel::save($eduData);
            if ($educationModel) return succMsg((array)'添加成功');
            // 存入失败
            return  errMsg('添加失败');
        }catch (\Exception $e){
            return errMsg($e->getMessage());
        }
    }

    // 修改教育经历
    public function educationalEdit(Request $request)
    {
        // 接收修改的值
        $data = $this->request->post();
        // 修改教育经历的id
        $id = $data['id'];
        try {
            // 查询需要修改的的教育经历
            $edu = EducationModel::where('id',$id)->find();
            if (empty($edu)) return errMsg('出现的一些问题，请给客服反馈');
            // 查询出基本用户信息
            $user = UserGxModel::where('phone',$request->phone)->value('id');
            $resume = ResumeModel::where('user_id',$user)->find();
            // 关联了学校表id，用户输入学校名称搜索出学校的id
            $school = $data['school_name'];
            $schoolModel = SchoolModel::where('name','=',$school)->field('id,name,type')->find();
            $eduModel = EducationModel::where('id',$id)->find();
            if ($eduModel['school_id'] == $schoolModel['id']) return errMsg('无需修改');
            // 存储数据
            $eduData = [
                'school_id' => $schoolModel['id'],
                'resume_id' => $resume['id'],
                'speciality' => $data['speciality'],
                'qualifications' => $data['qualifications'],
                'create_time' => date('Y-m-d H:i:s'),
                'end_time' => date('Y-m-d H:i:s'),
            ];
            $eduModel = $eduModel->where('id',$id)->update($eduData);
            if ($eduModel) return succMsg(array(),'修改成功');
            return errMsg('修改失败');
        }catch (\Exception $e){
            return errMsg($e->getMessage());
        }
    }

    // 教育经历详情
    public function educationalDetail(Request $request)
    {

        $user = UserGxModel::where('phone','=',$request->phone)->find();
        if ($user['type'] == 2) $this->error('失败哦');
        // 接收数据
        $id = $this->request->post('id/d');
        $educational = new EducationModel();
        $educational = $educational->where('id',$id)->find();
        if ($educational == null) $this->error('失败哦');
        $this->success($educational);
    }
}